var express = require('express')
    , route = express.Router()
    , logger = require('log4js').getLogger('data.history.js');

var list = require('./list');
var history = require('./history');

route.use((req, res, next) => {
    logger.info(req.originalUrl);
    next();
});

route.get('/', (req, res) => {
    res.send('get data!');
});

// http://localhost:3000/data/stockList?record=10&order=-code&page=1
route.get('/stockList', (req, res) => {
    list.getList(req, (err, rel) => {
        if (err) return res.send('internal server error!');
        res.send(rel);
    });
});

// http://localhost:3000/data/stockHistory?code=000001&record=10&order=-date
route.get('/stockHistory', (req, res) => {
    history.getHistory(req, (err, rel) => {
        if (err) {
            if (err.msg) {
                res.send(err.msg);
            }
            else {
                res.send('internal server error!');
            }
            logger.error({ url: req.originalUrl, err: err });
            return;
        }
        res.send(rel);
    });
});

module.exports = route;
